package com.geek.javase.ch09.work;

/*
 *@author wangpeng
 *2025/10/17 15:56
 *@description TODO
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * List作业
 * 1.模拟登陆，编写用户类，测试类。
 *   1>.给集合中存储5个用户对象。
 *   2>.从控制台输入用户名和密码，和集合中的对象信息进行比较，相同表示成功，不同表示失败。
 *   3>.有三次输入机会。
 */
public class Test1 {
    public static void main(String[] args) {
        List<User> list = new ArrayList();
        User user1 = new User("路人1", "123");
        User user2 = new User("路人2", "456");
        User user3 = new User("路人3", "123");
        User user4 = new User("路人4", "123");
        User user5 = new User("路人5", "123");

        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        System.out.println(list);
        login(list);
        //Non-static method 'login(java.util.List<com.geek.javase.ch09.work.User>)' cannot be referenced from a static context
        //psvm这个是静态方法，在此调用非静态方法login()，需要有类的实例或者将login()变为静态方法
    }
    //登录
    public  static boolean login(List<User> list){
        Scanner sc = new Scanner(System.in);
       boolean flag = false;
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名：");
            String username = sc.nextLine();
            System.out.println("请输入用户密码：");
            String password = sc.nextLine();

            for (int j = 0; j < list.size(); j++) {
                User user = list.get(j);
                if(Objects.equals(username,user.getName()) && Objects.equals(password,user.getPassword())){
                    flag = true;
                    System.out.println("登录成功！");
                    break;
                }
            }
        }
        if(!flag){
            System.out.println("登录失败！");
        }
        return flag;
    }
}
class User{
    private  String name;
    private  String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }
    //重写equals和hashCode方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(name, user.name) && Objects.equals(password, user.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, password);
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    public String toString() {
        return "User{name = " + name + ", password = " + password + "}";
    }
}